package com.qhyt.utils;



import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class SimpleDateUtil {
    // 短日期格式
    public final static String DATE_FORMAT = "yyyy-MM-dd";

    // 长日期格式
    public final static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
    * 将日期转换为字符串(yyyy-MM-dd HH:mm:ss)
     */
    public static String date2Str(Date date) {
        return date2Str(date,SimpleDateUtil.TIME_FORMAT);
    }
    /**
    * 将日期转换为字符串
     */
    public static String date2Str(Date date,String format) {
        if(date != null) {
            if(StringUtils.isBlank(format)){
                format = SimpleDateUtil.DATE_FORMAT;
            }
            SimpleDateFormat sf = new SimpleDateFormat(format);
            return sf.format(date);
        }else
        {
            return "";
        }
    }
     public static Date str2Date(String dateStr,String format){
        if(StringUtils.isBlank(dateStr)){
            return null;
        }
        if(StringUtils.isBlank(format)){
            format = DATE_FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date result = null;
         try {
             result = sdf.parse(dateStr);
         } catch (ParseException e) {
             e.printStackTrace();
         }
         return result;
     }

    public static Calendar setActualMinimun(Calendar calendar, int... fields) {
        for(int i : fields){
            calendar.set(i,calendar.getActualMinimum(i));
        }
        return calendar;
    }
    public static Date setActualMinimun(Date date, int... fields) {
       Calendar calendar = Calendar.getInstance();
       calendar.setTime(date);
       setActualMinimun(calendar,fields);
       return calendar.getTime();
    }
    public static Calendar setActualMaximun(Calendar calendar, int... fields) {
        for(int i : fields){
            calendar.set(i,calendar.getActualMaximum(i));
        }
        return calendar;
    }

    public static Date setActualMaximun(Date date, int... fields) {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(date);
        setActualMaximun(calendar,fields);
        return calendar.getTime();
    }
}
